﻿using System;
using Microsoft.Xna.Framework;

namespace ThreeDContentPipeline
{
    public class MapWriter : ContentTypeWriter<MapContent>
    {
        public override void Write(MapContent value, ContentWriter writer)
        {
            writer.Write(value.Batches.Count);

            foreach (BatchContent batch in value.Batches)
            {
                writer.WriteExternalReference(batch.Texture1);

                if (batch.Texture2 != null)
                {
                    writer.Write(true);
                    writer.WriteExternalReference(batch.Texture2);
                }
                else
                {
                    writer.Write(false);
                }

                writer.WriteVertexBuffer(batch.VertexBuffer);
                writer.WriteIndexBuffer(batch.IndexBuffer);
            }

            writer.Write(value.Primitives.Count);

            foreach (PrimitiveContent primitive in value.Primitives)
            {
                writer.Write(primitive.Name);

                writer.Write(primitive.Points.Count);

                foreach (Vector3 point in primitive.Points)
                {
                    writer.Write(point);
                }

                writer.Write(primitive.Polygons.Count);

                foreach (PolygonContent polygon in primitive.Polygons)
                {
                    writer.WriteArray(polygon.Indices.ToArray());
                }
            }
        }

        public override string ContentReaderName()
        {
            return "ThreeDWorld.MapReader, ThreeDWorld";
        }
    }
}
